package com.gradecak.alfresco.mvc.converter;

import java.io.IOException;

import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.namespace.QName;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;

public class QnameSerializer extends JsonSerializer<QName> {

  private ServiceRegistry serviceRegistry;

  public QnameSerializer(ServiceRegistry serviceRegistry) {
    this.serviceRegistry = serviceRegistry;
  }

  @Override
  public void serialize(QName value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
    String prefixString = value.toPrefixString(serviceRegistry.getNamespaceService());
    jgen.writeString(prefixString);
  }

  @Override
  public Class<QName> handledType() {
    return QName.class;
  }

}
